package com.foreveross.dao.imp;

import java.util.List;

import org.springframework.stereotype.Component;

import com.foreveross.common.Constants;
import com.foreveross.common.SQLUtil;
import com.foreveross.dao.MessageDAO;
import com.foreveross.vo.HrOptimized;
import com.foreveross.vo.Message;

@Component("messageDAO")
public class MessageDAOImp extends SQLUtil implements MessageDAO {


	@Override
	public long findMessageListCount(String weixinId,String houseId,String title,String userId) throws Exception {
		StringBuffer sqlStr = new StringBuffer();
//		sqlStr.append(" select count(distinct a.id) from message a ");
//		sqlStr.append(" where 1=1 ");
		sqlStr.append(" select count(model) from Message model where 1=1 ");
		if(!"".equals(weixinId) && null != weixinId){
			sqlStr.append(" and model.id in(select message from MessageHouse where house in ");
			sqlStr.append("(select b.hrHouses from HouseUser b where wechatId= '"+weixinId+"'))");
		}
		if(null!=houseId && !"".equals(houseId)){
			//sqlStr.append(" and ( a.id in (select message_id from message_houses where house_id ='" + houseId + "')");
			//sqlStr.append(" or  a.is_all ='"+ Constants.IS_ALL + "')");
			sqlStr.append(" and model.id in(select message from MessageHouse where house = '" + houseId + "')");
		}
		if(null!=title && !"".equals(title)){
			//sqlStr.append(" and a.title like '%" + title + "%'");
			sqlStr.append(" and model.title like '%" + title + "%'");
		}
		if (userId != null && !"".equals(userId)) {
			sqlStr.append(" and model.id in (select message from MessageHouse where house ");
			sqlStr.append("  in (select rh.hrHouses from SysRoleHouse rh where rh.sysRole in (select ur.sysRole from  SysUserRole ur where ur.sysUser='"
					+ userId + "')))");
//			sqlStr.append(" and model.createUser = '" + userId + "'");
		}
		sqlStr.append(" or  model.isAll ='"+ Constants.IS_ALL + "'");
		return Long.parseLong(findObejctByHql(sqlStr.toString()).toString());
	}

	@Override
	public List<Message> findMessageList(String weixinId,String houseId,String title, int pageIndex,
			int pageSize,String userId) throws Exception {
		StringBuffer sqlStr = new StringBuffer();
		sqlStr.append(" select model from Message model where 1=1 ");
		/*sqlStr.append(" select distinct a.* ");
		sqlStr.append(" from message a   ");
		sqlStr.append(" where 1=1 ");*/
		if(!"".equals(weixinId) && null != weixinId){
			sqlStr.append(" and model.id in(select message from MessageHouse where house in ");
			sqlStr.append("(select b.hrHouses from HouseUser b where wechatId= '"+weixinId+"'))");
		}
		if(null!=houseId && !"".equals(houseId)){
			//sqlStr.append(" and ( a.id in (select message_id from message_houses where house_id ='" + houseId + "')");
			//sqlStr.append(" or  a.is_all ='"+ Constants.IS_ALL + "')");
			sqlStr.append(" and model.id in(select message from MessageHouse where house = '" + houseId + "')");
			
		}
		if(null!=title && !"".equals(title)){
			sqlStr.append(" and model.title like '%" + title + "%'");
		}
		if (userId != null && !"".equals(userId)) {
			sqlStr.append(" and model.id in (select message from MessageHouse where house ");
			sqlStr.append("  in (select rh.hrHouses from SysRoleHouse rh where rh.sysRole in (select ur.sysRole from  SysUserRole ur where ur.sysUser='"
					+ userId + "')))");
//			sqlStr.append(" and model.createUser = '" + userId + "'");
		}
		sqlStr.append(" or  model.isAll ='"+ Constants.IS_ALL + "'");
		
		sqlStr.append(" order by model.createDate desc");
		return (List<Message>) findObjectAll(sqlStr.toString(), pageIndex,pageSize);
	}

	public void deleteMessage(String id) throws Exception{
		final String hql = "delete from Message t where t.id='" + id + "'";
		updateObejctByHql(hql);
	}

}
